package com.mbc.web.common.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.web.common.shared.HttpStatus;
import com.mbc.common.entities.CurrencyRate;
import com.mbc.web.common.client.ClientSingleton;
import com.mbc.web.common.client.CommonControllerMgr;
import com.mbc.web.common.client.CommonMessage;
import com.mbc.web.common.client.ViewUtils;
import com.mbc.web.common.client.ui.control.GridLabelColumn;
import com.mbc.web.common.client.ui.control.GridView;
import com.mbc.web.common.client.ui.control.MButton;
import com.mbc.web.common.client.ui.control.Pager;
import com.mbc.web.common.shared.dto.CurrencyRateDataListDTO;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;

public class SearchCurrencyRate extends AbstractListView<CurrencyRate> {

	@UiField GridView gvDisplay;
	@UiField Button btnSearch;
	@UiField Pager pager;
	@UiField TextBox txtKeyword;
	@UiField MButton btnAddNew;
	@UiField ListBox lstfield;
	
	public static final byte SAVE = 1;
	public static final byte DELETE = 0;
		
	private GridLabelColumn colRateCode = new GridLabelColumn();
	private GridLabelColumn colDescription = new GridLabelColumn();
	
	private static SearchCurrencyRateUiBinder uiBinder = GWT
			.create(SearchCurrencyRateUiBinder.class);

	interface SearchCurrencyRateUiBinder extends
			UiBinder<Widget, SearchCurrencyRate> {
	}

	public SearchCurrencyRate() {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
	}
	
	public SearchCurrencyRate(String message, byte status){
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
		if(status == SAVE)
			ViewUtils.showSuccessMessage(message + " " + CommonMessage.saved_message);
		else if(status == DELETE)
			ViewUtils.showSuccessMessage(message + " " + CommonMessage.deleted_message);
	}
		
	public SearchCurrencyRate(String title){
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
		this.title = title;
	}

	@Override
	protected void initialize() {
		
		lstfield.addItem("Currency Rate Code","CurrencyRateCode");
		lstfield.addItem("Description","Description");
		
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			
			@Override
			public void execute() {
				txtKeyword.setFocus(true);
			}
		});
		
		colRateCode.header = "Rate Id";
		colRateCode.styleName = "clickable-col";
		colRateCode.clickHandler = new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				CommonControllerMgr.findCurrencyRate(dataList.get(gvDisplay.getSelectedRowIndex()).getCurrencyRateId(), findCurrencyRateCallback());
				//MBC Begin
				closeModal();
				//End
			}
		};
		gvDisplay.addColumn(colRateCode);
		
		colDescription.header = "Description";
		gvDisplay.addColumn(colDescription);
		
		pager.btnFirst_ClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});
		
		pager.btnPrevious_ClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});
		
		pager.btnNext_ClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});
		
		pager.btnLast_ClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});
		onBtnSearchClick(null);
	}
	
	private AsyncCallback<CurrencyRate> findCurrencyRateCallback(){
		return new AsyncCallback<CurrencyRate>() {
			
			@Override
			public void onSuccess(CurrencyRate result) {
				if(result != null){
					if(isModal)
						selectedEntity = result;
					else
						ClientSingleton.changeView(new FrmCurrencyRate(result));
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		};
	}

	@Override
	protected void bindDisplay(List<CurrencyRate> list) {
		gvDisplay.removeAllRows();
		for (CurrencyRate l_currencyRate : list) {
			gvDisplay.addRow(new String[]{l_currencyRate.getCurrencyRateCode(), l_currencyRate.getDescription()});
		}
		generateList();
	}

	@Override
	protected void search() {
		ViewUtils.startAjaxLoader(CommonMessage.loading_message);
		CommonControllerMgr.findCurrencyRates(prepareCri(), findCurrencyRatesCallback());
	}
	
	private AsyncCallback<CurrencyRateDataListDTO> findCurrencyRatesCallback(){
		return new AsyncCallback<CurrencyRateDataListDTO>() {
			
			@Override
			public void onSuccess(CurrencyRateDataListDTO result) {
				ViewUtils.stopAjaxLoader();
				if(result.getHttpStatus() == HttpStatus.OK){
					dataList = result.getCurrencyRateDataList().getEntityList();
					pager.totalRecord = result.getCurrencyRateDataList().getTotalRecord();
					bindDisplay(dataList);
					pager.prepareVisible();
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		};
	}

	private CurrencyRate prepareCri()
	{
		CurrencyRate l_cri = new CurrencyRate();
		l_cri.setKeyword(txtKeyword.getValue());
		l_cri.setIndex(pager.index);
		l_cri.setSize(10);
		l_cri.setFieldname(lstfield.getValue(lstfield.getSelectedIndex()));
		return l_cri;
	}
	
	@UiHandler("btnAddNew")
	void onBtnAddNewClick(ClickEvent event) {
		ClientSingleton.changeView(new FrmCurrencyRate());
	}
	
	@UiHandler("btnSearch")
	void onBtnSearchClick(ClickEvent event) {
		pager.index = 0;
		search();
	}
	
	@UiHandler("txtKeyword")
	void onTxtKeywordKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
			onBtnSearchClick(null);
	}
}
